*This program computes descriptive statistics and graphs
*The program is run for the project on consumption uncertainty and precautionary saving using DNB data
*November 2018 - D. Christelis, D. Georgarakos, T. Jappelli, M. van Rooij

*set trace on
version 13.1
clear all
macro drop _all
set matsize 11000
set maxvar 10000
set more off

*ATTENTION - ATTENTION - ATTENTION 
*The following lines define the file path. Please adapt to own computer
do "c:/foldp/foldp.do"
global projf "${foldp}/DNB_survey_consumption/ConGrowth/Replication"

*Local of the distribution used
local dist sptri

*log 
cap log close
log using "${projf}/Programs/Descriptive/Descriptive_stats_graphs.log", replace

global stdate $S_DATE 
global sttime $S_TIME
noisily display "Begin program at ${sttime} on ${stdate}"   /* time stamp */

*Setting the scheme
global schm s1mono

*Read the person-level data in long form
use "${projf}/Data/Data_cons_uncertainty.dta", clear

*Using only the observations that appear in the regression
qui keep if hpconch_nyr!=. & hm`dist'_sqcgr!=. & regio!=.

*Generating integer weights
qui gen double intwgtp=round(wgth*1000)

*Fig. 1a Histogram of the minimum of future consumption level
hist hmin_sp if hmin_sp<8000 [fw=intwgtp], bin(30) scheme(${schm}) ytitle(,margin(medium)) ///
   xtitle("Minimum expected consumption level",margin(medium)) ytitle("Sample proportion") /*freq*/frac /*ylabel(.0001 .0003 .0005)*/ ///
   xlabel(0(2000)8000, format(%5.0fc)) saving("${projf}/Results/Graphs/temp1.gph", replace)
*graph export "${projf}/Results/Graphs/Fig1_min_`dist'.wmf", replace     

*Fig. 1b Histogram of the maximum of future consumption level
hist hmax_sp if hmax_sp<8000 [fw=intwgtp], bin(30) scheme(${schm}) ytitle(,margin(medium)) ///
   xtitle("Maximum expected consumption level",margin(medium)) ytitle("Sample proportion") /*freq*/frac /*ylabel(.0001 .0003 .0005)*/ ///
   xlabel(0(2000)8000, format(%5.0fc)) saving("${projf}/Results/Graphs/temp2.gph", replace)
*graph export "${projf}/Results/Graphs/Fig2_max_`dist'.wmf", replace     

*Fig. 1c Histogram of the probability of being above the average of the min and max values
*of the future consumption level
hist hpi_sp [fw=intwgtp], scheme(${schm}) ytitle(,margin(medium)) ///
   xtitle("Probability",margin(medium)) /*ylabel()*/ ytitle("Sample proportion") /*freq*/frac ///
   xlabel(0(.1)1, format(%3.1fc)) saving("${projf}/Results/Graphs/temp3.gph", replace)
*graph export "${projf}/Results/Graphs/Fig3_prob_`dist'.wmf", replace     

*Fig. 1d Histogram of the expected square of consumption growth
hist w005_hm`dist'_sqcgr if w005_hm`dist'_sqcgr<.05 [fw=intwgtp], bin(30) scheme(${schm}) ytitle("Sample proportion") /*freq*/frac ///
   xtitle("Expected square of consumption growth",margin(medium)) /*ylabel()*/ ///
   xlabel(0(.01).05, format(%4.2fc)) saving("${projf}/Results/Graphs/temp4.gph", replace)


gr combine "${projf}/Results/Graphs/temp1.gph" ///
           "${projf}/Results/Graphs/temp2.gph" ///
           "${projf}/Results/Graphs/temp3.gph" ///
           "${projf}/Results/Graphs/temp4.gph", scheme(${schm}) saving("${projf}/Results/Graphs/Fig1.gph", replace)
graph export "${projf}/Results/Graphs/Fig1_`dist'.wmf", replace     

erase "${projf}/Results/Graphs/temp1.gph"
erase "${projf}/Results/Graphs/temp2.gph"
erase "${projf}/Results/Graphs/temp3.gph"
erase "${projf}/Results/Graphs/temp4.gph"
erase "${projf}/Results/Graphs/Fig1.gph"

*Fig. 2. Expected square of consumption growth by age categories 

*Age categories
gen byte agecat = recode(age,30,40,50,60,70,80)

preserve

collapse (p50) w005_hsd`dist'_consgr w005_hm`dist'_sqcgr w005_hpconch_nyr [aw=wgth],by(agecat)

qui twoway (scatter w005_hm`dist'_sqcgr agecat, mlabsize(vsmall)) (lfit w005_hm`dist'_sqcgr agecat), legend(off) scheme(${schm}) ///
   ytitle("Median expected square of" "consumption growth", margin(medium)) xtitle("Age",margin(medium)) ylabel(0.0(.002).008) ///
   xlabel(30(10)80, format(%3.0fc)) title(" ") note(" ") saving("${projf}/Results/Graphs/temp1.gph", replace)
graph export "${projf}/Results/Graphs/Fig2_`dist'.wmf", replace     
erase "${projf}/Results/Graphs/temp1.gph"

restore


*Fig. 3 Expected consumption growth by values of expected square of consumption growth

*Categories of expected square of of consumption growth based in deciles
_pctile w005_hm`dist'_sqcgr [aw=wgth],nq(10)
gen double m_sqcgr_cat = recode(w005_hm`dist'_sqcgr,r(r1),r(r2),r(r3),r(r4),r(r5),r(r6),r(r7),r(r8),r(r9))

preserve

collapse (mean) w005_hpconch_nyr [aw=wgth],by(m_sqcgr_cat)

qui twoway (scatter w005_hpconch_nyr m_sqcgr_cat, mlabsize(vsmall)) (lfit w005_hpconch_nyr m_sqcgr_cat), scheme(${schm}) legend(off) ///
   ytitle("Average of expected consumption growth", margin(medium)) ///
   xtitle("Levels of expected square" "of consumption growth",margin(medium)) ylabel(-.02(.02).06) ///
   xlabel(0(.005).025, format(%5.3fc)) saving("${projf}/Results/Graphs/temp1.gph", replace)
graph export "${projf}/Results/Graphs/Fig3_`dist'.wmf", replace     
erase "${projf}/Results/Graphs/temp1.gph"

restore

*Fig. A2 Expected consumption growth by values of the expected square of income growth

*Categories of expected square of of income growth based in deciles
_pctile w005_hm`dist'_sqincgr [aw=wgth],nq(10)
gen double m_sqincgr_cat = recode(w005_hm`dist'_sqincgr,r(r1),r(r2),r(r3),r(r4),r(r5),r(r6),r(r7),r(r8),r(r9))

preserve

collapse (mean) w005_hpconch_nyr [aw=wgth],by(m_sqincgr_cat)

qui twoway (scatter w005_hpconch_nyr m_sqincgr_cat, mlabsize(vsmall)) (lfit w005_hpconch_nyr m_sqincgr_cat), scheme(${schm}) legend(off) ///
   ytitle("Average of expected consumption growth", margin(medium)) ///
   xtitle("Levels of expected square" "of income growth",margin(medium)) ylabel(-.02(.02).06) ///
   xlabel(0(0.03).3, format(%5.2fc)) saving("${projf}/Results/Graphs/temp1.gph", replace)
graph export "${projf}/Results/Graphs/FigA2_`dist'.wmf", replace     
erase "${projf}/Results/Graphs/temp1.gph"

restore

*Table 1 Descriptive statistics
tabstat hmin_sp hmax_sp hpi_sp w005_hpconch_nyr w005_hsd`dist'_consgr w005_hm`dist'_sqcgr w005_hsd`dist'_incgr age ///
    fem hhsize couple [aw=wgth],statistics(mean p50 sd) columns(statistics) save
tabstatmat A
mat A = A'
local rn = colsof(A)
mat B = J(1,`rn',.)
qui count
mat B[1,1]=r(N)
mat A = (A\B)

mat list A

mat2txt, mat(A) saving("${projf}/Results/Descriptive/Descriptive_Statistics_`dist'.txt") replace format(%15.6f) ///      
  title("Descriptive Statistics, $S_DATE, $S_TIME")      


*Time stamp
noisily display "Program started at: ${sttime} of ${stdate}"      /* beginning time stamp */
noisily display "Program finished at: $S_TIME of $S_DATE"       /*ending time stamp*/       

cap log close
